package org.phoenix.mall.admin.service.impl;

import io.swagger.annotations.ApiOperation;
import org.phoenix.mall.admin.common.pojo.vo.AttributeVO;
import org.phoenix.mall.admin.common.utils.PhoenixResponseJSON;
import org.phoenix.mall.admin.mapper.AttributeMapper;
import org.phoenix.mall.admin.pojo.Attribute;
import org.phoenix.mall.admin.service.AttributeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AttributeServiceImpl implements AttributeService {

    private AttributeMapper attributeMapper;

    @Override
    public int create(Attribute attribute) {
        return attributeMapper.insert(attribute);
    }

    @Override
    public int edit(Attribute attribute) {
        return attributeMapper.updateByPrimaryKeySelective(attribute);
    }

    @Override
    public int delete(Attribute attribute) {
        return attributeMapper.delete(attribute);
    }

    @Override
    public List<AttributeVO> getAttributeList(Integer categoryId, String sel) {
        Example example = new Example(Attribute.class);
        example.createCriteria().andEqualTo("categoryId",categoryId).andEqualTo("attrSel",sel);
        List<Attribute> list = attributeMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)){
            List<AttributeVO> reList = new ArrayList<>();
            AttributeVO attrVO;
            for (Attribute item : list){
                attrVO = new AttributeVO();
                BeanUtils.copyProperties(item,attrVO);
                reList.add(attrVO);
            }

            return reList;
        }
        return null;
    }

    @Resource
    public void setAttributeMapper(AttributeMapper attributeMapper) {
        this.attributeMapper = attributeMapper;
    }
}
